﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace MapleLeaf.Pay {
    // Token: 0x0200000D RID: 13
    public static class AesUtil {
        // Token: 0x06000063 RID: 99 RVA: 0x000041E8 File Offset: 0x000023E8
        public static T Use<T>(this T aes, string key, string vector = "Lvren.AesUnit") where T : Aes {
            aes.Key = SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(key));
            aes.IV = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(vector));
            return aes;
        }

        // Token: 0x06000064 RID: 100 RVA: 0x00004238 File Offset: 0x00002438
        public static byte[] Encrypt(this Aes aes, string text) {
            return aes.Encrypt(text, Encoding.UTF8);
        }

        // Token: 0x06000065 RID: 101 RVA: 0x00004254 File Offset: 0x00002454
        public static byte[] Encrypt(this Aes aes, string text, Encoding encoding) {
            return aes.Encrypt(encoding.GetBytes(text));
        }

        // Token: 0x06000066 RID: 102 RVA: 0x00004270 File Offset: 0x00002470
        public static byte[] Encrypt(this Aes aes, byte[] bytes) {
            byte[] result;
            using (aes.CreateEncryptor()) {
                using (MemoryStream memoryStream = new MemoryStream()) {
                    using (CryptoStream cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write)) {
                        cryptoStream.Write(bytes, 0, bytes.Length);
                    }
                    result = memoryStream.ToArray();
                }
            }
            return result;
        }

        // Token: 0x06000067 RID: 103 RVA: 0x000042FC File Offset: 0x000024FC
        public static string Decrypt(this Aes aes, byte[] bytes) {
            return aes.Decrypt(bytes, Encoding.UTF8);
        }

        // Token: 0x06000068 RID: 104 RVA: 0x00004318 File Offset: 0x00002518
        public static string Decrypt(this Aes aes, byte[] bytes, Encoding encoding) {
            string result = null;
            using (ICryptoTransform cryptoTransform = aes.CreateDecryptor()) {
                using (MemoryStream memoryStream = new MemoryStream(bytes)) {
                    using (CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Read)) {
                        using (StreamReader streamReader = new StreamReader(cryptoStream, encoding)) {
                            result = streamReader.ReadToEnd();
                        }
                    }
                }
            }
            return result;
        }

        // Token: 0x06000069 RID: 105 RVA: 0x000043BC File Offset: 0x000025BC
        public static string EncryptToken(this Aes aes, params object[] values) {
            return aes.Encrypt(string.Join("\0", values)).UrlBase64Encode();
        }

        // Token: 0x0600006A RID: 106 RVA: 0x000043E4 File Offset: 0x000025E4
        public static string[] DecryptToken(this Aes aes, string token) {
            aes.Decrypt(token.UrlBase64Decode());
            return aes.Decrypt(token.UrlBase64Decode()).Split(new char[1]);
        }
    }
}
